home *** CD-ROM | disk | FTP | other *** search
- /*
- MountainsToolbox.c
- The toolbox interface to front-end MountainsAlgorithm.c
- By Ben Haller 1/1/90
- ©1990 Ben Haller
- */
-
- #include "Mountains.h"
-
- char done=FALSE; /* FALSE as long as user has not quit */
- MenuHandle menu[6]; /* Our menus */
- EventRecord evt; /* Our eventrecord */
- WindowPtr wp; /* Our window */
- CursHandle watch; /* Watch cursor for long waits */
- char colorF; /* TRUE if 8-bit color */
- int wx,wy,wd; /* Size of window (x and y dimensions) */
- Rect r; /* scratch rectangle for general use */
- char menuCk[5]={3,6,5,5,1}; /* number of checked item in each menu */
- /* (File menu not included) */
-
- /********************************************************/
- /* Routines in MountainsAlgorithm.c */
- void DrawMountains(void);
- void CalcMountains(void);
-
- /********************************************************/
- /* Initializes the toolbox and all important variables, */
- /* loads resources, etc. */
- void Init()
- {
- int n;
-
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
- FlushEvents(-1, 0);
-
- /* Reseed the random number generator. Note: You can force generation */
- /* of a specific series of random numbers (and thus a specific picture, */
- /* all parameters being equal) by replacing this statement with */
- /* "randSeed=<any integer>" */
- randSeed=Ticks;
-
- /* Find out if we're running in 8-bit color and set colorF accordingly */
- n=(*((*GetGDevice())->gdPMap))->pixelSize;
- colorF=(n>=8);
-
- /* Make menus */
- ClearMenuBar();
- menu[0]=NewMenu(5000,"\pFile");
- AppendMenu(menu[0],"\pCalculate;(-;Quit");
- InsertMenu(menu[0],0);
- menu[1]=NewMenu(5001,"\pIterations");
- AppendMenu(menu[1],"\p3;4;5;6;7;8;9");
- InsertMenu(menu[1],0);
- menu[2]=NewMenu(5002,"\pContour");
- AppendMenu(menu[2],"\p0.25;0.50;0.75;1.00;1.25;1.50;1.75;2.00;3.00;5.00");
- InsertMenu(menu[2],0);
- menu[3]=NewMenu(5003,"\pSmoothness");
- AppendMenu(menu[3],"\p1.00;1.25;1.50;1.75;2.00;2.25;2.75;3.50;5.00;∞");
- InsertMenu(menu[3],0);
- menu[4]=NewMenu(5004,"\pHeight");
- AppendMenu(menu[4],"\p1;2;3;4;5;6;7;8;9;10");
- InsertMenu(menu[4],0);
- menu[5]=NewMenu(5005,"\pProfile");
- AppendMenu(menu[5],"\p#1;#2;#3;#4");
- InsertMenu(menu[5],0);
- DrawMenuBar();
-
- /* Check all the menu items appropriate */
- for (n=1; n<6; n++)
- CheckItem(menu[n],menuCk[n-1],TRUE);
-
- /* Get watch */
- watch=GetResource('CURS',4);
- HLock(watch);
-
- /* Make our window, full screen inset 5 pixels */
- r=screenBits.bounds;
- r.top+=MBarHeight;
- InsetRect(&r,5,5);
- wx=r.right-r.left;
- wy=r.bottom-r.top;
- if (wy>wx/2) wy=wx/2;
- else if (wx>wy*2) wx=wy*2;
- wd=wx;
- r.bottom=r.top+wy;
- r.right=r.left+wx;
- if (colorF)
- wp=NewCWindow(0L, &r, "\p", TRUE, plainDBox, -1L, FALSE, 0L);
- else
- wp=NewWindow(0L, &r, "\p", TRUE, plainDBox, -1L, FALSE, 0L);
- }
-
- /********************************************************/
- /* Handle the selection of a menu item */
- void HandleMenu(m,i)
- int m,i; /* m=menu id, i=item number */
- {
- m-=5000;
- switch (m) {
- case 0:
- if (i==1) { /* Recalculate and update */
- CalcMountains();
- InvalRect(&(wp->portRect));
- } else done=TRUE; /* Quit */
- break;
- case 1:
- case 2:
- case 3:
- case 4:
- case 5: /* Move the check */
- CheckItem(menu[m],menuCk[m-1],FALSE);
- menuCk[m-1]=i;
- CheckItem(menu[m],menuCk[m-1],TRUE);
- break;
- }
- if (!done) HiliteMenu(0);
- }
-
- /********************************************************/
- /* Handle updating our window */
- void HandleUpdate(wp)
- WindowPtr wp;
- {
- SetPort(wp);
- BeginUpdate(wp);
- if (colorF) {
- RGBColor blackc;
-
- blackc.red=0; blackc.green=0; blackc.blue=0;
- RGBForeColor(&blackc);
- }
- FillRect(&(wp->portRect),black);
- DrawMountains();
- EndUpdate(wp);
- }
-
- /********************************************************/
- /* Handle a mouse click */
- void HandleMouse()
- {
- WindowPtr wW;
- long msresult;
-
- switch (FindWindow(evt.where, &wW)) {
- case inMenuBar:
- msresult=MenuSelect(evt.where);
- HandleMenu(HiWord(msresult),LoWord(msresult));
- break;
- default: break;
- }
- }
-
- /********************************************************/
- /* Initialize, get events until done. */
- int main()
- {
- Init();
- while (!done) {
- SystemTask();
- if (GetNextEvent(everyEvent, &evt)) {
- switch (evt.what) {
- case mouseDown:
- HandleMouse();
- break;
- case updateEvt:
- HandleUpdate(evt.message);
- break;
- }
- }
- }
- }